import { http } from '@/utils/http/axios'

import { useProjectSettingStore } from '@/store/modules/projectSetting'

// import { langConfig } from '@/utils/http/axios/index';

// const langConfig = {}

// 根据字典类型查询系统类(后台管理系统中的字典表)字典数据信息
export function getDictData(dictTypeCode = '', _type = 'crm') {
  const projectSettingStore = useProjectSettingStore()

  const language = projectSettingStore.language

  return http.request({
    url: `/admin/sys/dictionary/info`,
    method: 'post',
    data: {
      languageType: language,
      dictionaryTypeCode: dictTypeCode,
    },
  })
}

// 获取临时下载链接
export function downLoadFile(obj) {
  const { fileUrl = '', fileName, getDownloadUrl = '/oss/file/temp/url', type = null } = obj
  // const urlArr = fileUrl.split('||');
  let params: any = {
    url: fileUrl,
  }

  let method = 'post'

  if (type) {
    params = {
      type,
    }

    method = 'get'
  }

  return http
    .request({
      url: getDownloadUrl,
      method,
      params,
    })
    .then(async (res) => {
      let url = res?.row?.temUrl

      if (url) {
        url = url.replace('http:', window.location.protocol)

        url = url.replace('https:', window.location.protocol)

        http.request(
          {
            url,
            method: 'get',
            dataType: 'jsonp',
            responseType: 'blob',
            fileName,
            headers: {
              isToken: false,
            },
          },
          { withToken: false },
        )
      }
    })
    .catch((err) => {
      console.log('err', err)
    })
  // 下载不调后端接口，前端处理
  /*
    .then((tempRes) => {

      return request({
        url: `/oss/file/download?fileName=${tempRes?.row?.temUrl}`,
        method: 'get',
        responseType: 'blob',
        fileName
      });

    });
      */
}

// 通用下载模板方法
export function downLoadFile2(fileName) {
  return http.request(
    {
      url: `/crm/ex/common/download/file/${encodeURI(fileName)}`,
      method: 'post',
      responseType: 'blob',
    },
    {
      isTransformResponse: false,
    },
  )
}

// 获取表格列数据
export function getFieldInfoList(data) {
  return http.request({
    url: '/admin/sys/pageColumn/list',
    method: 'post',
    data,
  })
}

// 获取表格列数据
export function setFieldInfoList(data) {
  return http.request({
    url: '/admin/sys/pageColumn/update',
    method: 'post',
    data,
  })
}

// 获取临时链接
export function getTemUrl(url) {
  return http.request({
    url: '/oss/file/temp/url',
    method: 'post',
    params: {
      url,
    },
  })
}

// 获取临时链接
export function fileUpload(data) {
  return http.request({
    url: '/file/upload',
    method: 'post',
    data,
    headers: {
      'Content-Type': 'multipart/form-data',
      isUploadFile: true,
    },
  })
}

// 获取临时链接
export function getMac() {
  return http.request({
    url: '/getMac',
    method: 'post',
  })
}

// 上传调机图片
export function uploadInterface(data) {
  return http.request({
    url: '/file/uploadInterface',
    method: 'post',
    data,
    headers: {
      'Content-Type': 'multipart/form-data',
      isUploadFile: true,
    },
  })
}